package com.shell.governor.user.rx;

import com.shell.bee.entity.auth.AuthOrgan;
import com.shell.framework.rx.OnErrorNotImplementedSubscriber;
import com.shell.framework.utils.CommonDataUtil;
import com.shell.governor.user.UserEvent;
import com.shell.governor.user.dao.PubUserOrganDao;
import com.shell.governor.user.po.PubUser;
import com.shell.governor.user.po.PubUserOrgan;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class SetUserOrganSubscriber extends OnErrorNotImplementedSubscriber<UserEvent> {
	@Autowired
	PubUserOrganDao userOrganDao;

	public void onApplicationEvent(UserEvent event) {
		PubUser user = event.getUser();
		PubUserOrgan userOrgan = new PubUserOrgan();
		AuthOrgan ent = CommonDataUtil.getCurEnt();
		userOrgan.setEntId(ent.getOrganId());
		userOrgan.setUserId(user.getUserId());
		List<PubUserOrgan> uos = this.userOrganDao.select(userOrgan);
		for (PubUserOrgan uo : uos) {
			this.userOrganDao.delete(uo);
		}
		userOrgan.setDeptId(ent.getOrganId());
		userOrgan.setDeptName(ent.getOrganName());
		userOrgan.setEntName(ent.getOrganName());
		userOrgan.setEntType("ENT");
		userOrgan.setValid("1");
		this.userOrganDao.insert(userOrgan);
	}
}
